package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.role.mapper.RoleRelUserMapper;
import com.originskyseed.platform.role.model.RoleRelUser;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.TaskSelectSaleManVO;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 用户信息表Service接口实现类（OSI_USER_INFO）
 *
 * @author wlj
 */
@DubboService
@Component("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {

	@Resource
	private UserInfoMapper userInfoMapper;

	@Resource
	private RoleRelUserMapper roleRelUserMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	/*
	 * @Override public int deleteByPrimaryKey(String id) throws Exception { return this.userInfoMapper.deleteByPrimaryKey(id); }
	 */

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	/*
	 * @Override public int deleteByPrimaryKeys(List<String> ids) throws Exception { return this.userInfoMapper.deleteByPrimaryKeys(ids); }
	 */

	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return 0;
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(UserInfo record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userInfoMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(UserInfo record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userInfoMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(UserInfo record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userInfoMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(UserInfo record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userInfoMapper.updateByPrimaryKeySelective(record);
	}


	/**
	 * 更新员工等级信息
	 * @param id
	 * @param levelId
	 * @return
	 * @throws Exception
	 */
	public int updateLevelIdByUserId(String id,String levelId) throws Exception
	{
		return this.userInfoMapper.updateLevelIdByUserId(id,levelId,new Date());
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserInfo selectByPrimaryKey(String id) throws Exception {
		return this.userInfoMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserInfo selectByPrimaryKeyTenantCode(String id, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectByPrimaryKey(id);
	}

	@Override
	public UserInfo selectByAccount(String account) throws Exception {
		return this.userInfoMapper.selectByAccount(account);
	}

	@Override
	public UserInfo selectAllByAccount(String account) throws Exception {
		return this.userInfoMapper.selectAllByAccount(account);
	}

	@Override
	public UserInfo selectByName(String username) throws Exception {
		return this.userInfoMapper.selectByName(username);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> selectList(UserInfo record) throws Exception {
		return this.userInfoMapper.selectList(record);
	}

	/**
	 * 查询导出列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> selectExportExcelList(UserInfo record) throws Exception {
		return this.userInfoMapper.selectExportExcelList(record);
	}

	/**
	 * 培训任务查询新员工
	 *
	 * @return
	 */
	@Override
	public List<UserInfo> taskSelectNewUserList(Map<String, Object> map, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.taskSelectNewUserByTimeList(map);
	}

	/**
	 * 根据任务创建时间查询新员工
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> taskSelectNewUserByTimeList(Map<String, Object> map) throws Exception {
		return this.userInfoMapper.taskSelectNewUserList(map);
	}

	/**
	 * 根据部门ID查询用户
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectUserIdsByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectUserIdsByDeptIds(deptIdList);
	}

	/**
	 * 根据部门ID查询用户
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectAccountByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectAccountByDeptIds(deptIdList);
	}

	/**
	 * 根据部门ID查询主播id
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectAnchorIdsUserIdsByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectAnchorIdsUserIdsByDeptIds(deptIdList);
	}

	/**
	 * 根据部门ID查询业务员用户id
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectSalesmanUserIdsByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectSalesmanUserIdsByDeptIds(deptIdList);
	}

	/**
	 * 根据部门ID查询主播用户id
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectAnchorIdsByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectAnchorIdsByDeptIds(deptIdList);
	}

	/**
	 * 根据部门ID查询用户表中主播的第三方主键
	 *
	 * @param deptIdList
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectAnchorOtherAccountsByDeptIds(List<String> deptIdList) throws Exception {
		return this.userInfoMapper.selectAnchorOtherAccountsByDeptIds(deptIdList);
	}

	@Override
	public List<String> selectAnchorOtherAccountsByDeptIdsTenantCode(List<String> deptIdList, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectAnchorOtherAccountsByDeptIds(deptIdList);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserInfo> selectListByPage(Map<String, Object> params, BasePage<UserInfo> page) throws Exception {
		params.put("page", page);
		List<UserInfo> list = this.userInfoMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserInfo> selectListRelRoleByPage(Map<String, Object> params, BasePage<UserInfo> page) throws Exception {
		params.put("page", page);
		List<UserInfo> list = this.userInfoMapper.selectListRelRoleByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 选择人员信息
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserInfo> selectMaintainListByPage(Map<String, Object> params, BasePage<UserInfo> page) throws Exception {
		params.put("page", page);
		List<UserInfo> list = this.userInfoMapper.selectMaintainListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 选择业务员信息，全平台、公会下
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TaskSelectSaleManVO> selectSaleInfoListByPage(Map<String, Object> params, BasePage<TaskSelectSaleManVO> page) throws Exception {
		params.put("page", page);
		List<TaskSelectSaleManVO> list = this.userInfoMapper.selectSaleInfoListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 选择主播信息公会、全平台
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TaskSelectSaleManVO> selectMaintainAnchorListByPage(Map<String, Object> params, BasePage<TaskSelectSaleManVO> page) throws Exception {
		params.put("page", page);
		List<TaskSelectSaleManVO> list = this.userInfoMapper.selectMaintainAnchorListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广任务选择主播
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public BasePage<TaskSelectSaleManVO> selectPromotionTaskAnchorListByPage(Map<String, Object> params, BasePage<TaskSelectSaleManVO> page) throws Exception{
		params.put("page",page);
		List<TaskSelectSaleManVO> list = this.userInfoMapper.selectPromotionTaskAnchorListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据公会Id分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserInfo> selectListBytradeunionIdPage(Map<String, Object> params, BasePage<UserInfo> page) throws Exception {
		params.put("page", page);
		List<UserInfo> list = this.userInfoMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	@Override
	public List<UserInfo> selectUserInfoByOpenId(String openId) throws Exception {
		return userInfoMapper.selectUserInfoByOpenId(openId);
	}

	@Override
	public int deleteLogicallyByPrimaryKeys(List<String> ids) throws Exception {
		return userInfoMapper.deleteLogicallyByPrimaryKeys(ids);
	}

	@Override
	public int updateLogicallyByPrimaryKeys(List<String> ids) throws Exception {
		return userInfoMapper.updateLogicallyByPrimaryKeys(ids);
	}

	@Override
	public int updateStatus(List<String> ids, Integer status) throws Exception {
		return this.userInfoMapper.updateStatus(ids, status);
	}

	/**
	 * 签约或未签约
	 *
	 * @param ids
	 * @param isEconomicContract
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsEconomic(List<String> ids, Integer isEconomicContract) throws Exception {
		return this.userInfoMapper.updateIsEconomic(ids,isEconomicContract);
	}

	@Override
	public boolean existAccount(String account) throws Exception {
		return this.userInfoMapper.existAccount(account);
	}

	@Override
	public int updateUserInfoPassword(String account, String password) throws Exception {
		return this.userInfoMapper.updateUserInfoPassword(account, password);
	}

	@Override
	public UserInfo existUserInfoAccount(String account) throws Exception {
		return this.userInfoMapper.existUserInfoAccount(account);
	}

	@Override
	public List<UserInfo> selectMobileNumber(String mobileNumber) throws Exception {
		return this.userInfoMapper.selectMobileNumber(mobileNumber);
	}

	@Override
	public Boolean accountExist(UserInfo userInfo) throws Exception {
		return this.userInfoMapper.accountExist(userInfo);
	}

	@Override
	public Boolean nameExist(UserInfo userInfo) throws Exception {
		return this.userInfoMapper.nameExist(userInfo);
	}

	@Override
	public int updateByAccount(UserInfo userInfo) throws Exception {
		return this.userInfoMapper.updateByAccount(userInfo);
	}

	@Override
	public UserInfo selectByPhoneNum(String phone) throws Exception {
		return this.userInfoMapper.selectByPhoneNum(phone);
	}

	@Override
	public boolean existMobileNumber(String mobileNumber) throws Exception {
		return this.userInfoMapper.existMobileNumber(mobileNumber);
	}

	@Override
	public UserInfo selectByAccountAndPassword(String account, String password) throws Exception {
		return userInfoMapper.selectByAccountAndPassword(account, DigestUtils.md5DigestAsHex(password.getBytes()));
	}

	/**
	 * 更新登录信息
	 *
	 * @param id
	 * @param ip
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserInfo updateLoginInfo(String id, String ip) throws Exception {
		UserInfo record = this.selectByPrimaryKey(id); // 避免在SSOToken缓存中获取到旧的UserInfo信息来更新到数据库
		record.setFinallyLoginTime(new Date());
		record.setFinallyLoginIp(ip);
		this.userInfoMapper.updateByPrimaryKeySelective(record);
		return record;
	}

	/**
	 * 批量插入
	 *
	 * @param userInfoList
	 * @return
	 * @throws Exception
	 */
	public int insertBatchUser(List<UserInfo> userInfoList) throws Exception {
		return userInfoMapper.insertBatchUser(userInfoList);
	}

	/**
	 * 批量插入
	 *
	 * @param userInfoList
	 * @return
	 * @throws Exception
	 */
	public int insertBatchUser(List<UserInfo> userInfoList, List<RoleRelUser> roleRelUserList) throws Exception {
		this.roleRelUserMapper.insertBatch(roleRelUserList);
		return userInfoMapper.insertBatchUser(userInfoList);
	}

	/**
	 * 插入
	 *
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public int insertBatchUser(UserInfo userInfo, RoleRelUser roleRelUser) throws Exception {
		this.roleRelUserMapper.insertSelective(roleRelUser);
		return userInfoMapper.insertSelective(userInfo);
	}

	/**
	 * 查询员工编号
	 *
	 * @return
	 * @throws Exception
	 */
	public String selectUserNum() throws Exception {
		return this.userInfoMapper.selectUserNum();
	}

	/**
	 * 查询最大的优先级
	 *
	 * @return
	 * @throws Exception
	 */
	public Integer selectPrority() throws Exception {
		return this.userInfoMapper.selectPrority();
	}

	/**
	 * 根据优先级查询员工
	 *
	 * @param prority
	 * @return
	 * @throws Exception
	 */
	public UserInfo selectUserByPrority(Integer prority) throws Exception {
		return this.userInfoMapper.selectUserByPrority(prority);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downUser
	 * @param upUser
	 * @return
	 * @throws Exception
	 */
	public int updatePrority(UserInfo downUser, UserInfo upUser) throws Exception {
		downUser.setUpdateTime(new Date());
		upUser.setUpdateTime(new Date());
		this.userInfoMapper.updateByPrimaryKeySelective(downUser);
		return this.userInfoMapper.updateByPrimaryKeySelective(upUser);
	}

	/**
	 * 根据部门id查询员工数量
	 *
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	public int selectUserNumByDeptId(String deptId) throws Exception {
		return this.userInfoMapper.selectUserNumByDeptId(deptId);
	}

	/**
	 * 根据部门id查询员工数量
	 *
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	public int selectUserNumByStatusDeptId(String deptId) throws Exception {
		return this.userInfoMapper.selectUserNumByStatusDeptId(deptId);
	}

	/**
	 * 根据部门id集合查询员工数量,排除异常状态的
	 *
	 * @param deptIds
	 * @return
	 * @throws Exception
	 */
	public int selectUserNumByStatusDeptIds(List<String> deptIds) throws Exception{
		return this.userInfoMapper.selectUserNumByStatusDeptIds(deptIds);
	}

	/**
	 * 根据部门查询成员
	 *
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> selectUserByDeptId(String deptId) throws Exception {
		return this.userInfoMapper.selectUserByDeptId(deptId);
	}

	/**
	 * 查询第三方主键是否存在
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<String> selectRepeatOtherId(Map<String, Object> map) throws Exception {
		return this.userInfoMapper.selectRepeatOtherId(map);
	}

	/**
	 * 根据用户id查询用户id第三方主键
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public String selectOtherAccountById(String id) throws Exception {
		return this.userInfoMapper.selectOtherAccountById(id);
	}

	/**
	 * 根据第三方主键查询用户
	 *
	 * @param otherAccount
	 * @return
	 * @throws Exception
	 */
	public UserInfo selectByOtherAccount(String otherAccount) throws Exception {
		return this.userInfoMapper.selectByOtherAccount(otherAccount);
	}


	/**
	 * 根据第三方主键查询用户
	 *
	 * @param otherAccount
	 * @return
	 * @throws Exception
	 */
	public UserInfo selectByOtherAccountTenantCode(String otherAccount, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.userInfoMapper.selectByOtherAccount(otherAccount);
	}

	/**
	 * 根据部门查询业务员id
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectSalesmanIdsByDeptIds(List<String> list) throws Exception {
		return this.userInfoMapper.selectSalesmanIdsByDeptIds(list);
	}

	/**
	 * 根据部门查询第三方账号
	 *
	 * @param type  1 内容 2 推广
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectOtherAccountByDeptIds(List<String> list,Integer type) throws Exception {
		return this.userInfoMapper.selectOtherAccountByDeptIds(list,type);
	}

	@Override
	public List<String> selectSalesmanIdsByDeptIdsTenantCode(List<String> list, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectSalesmanIdsByDeptIds(list);
	}

	/**
	 * 根据部门查询青芒主播id
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectAnchorOtherIdsByDeptIds(List<String> list) throws Exception {
		return this.userInfoMapper.selectAnchorOtherIdsByDeptIds(list);
	}

	/**
	 * 查询新增的主播数量
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Integer selectAnchorNumberByTime(Map<String, Object> map) throws Exception {
		return this.userInfoMapper.selectAnchorNumberByTime(map);
	}

	/**
	 * 邀请码是否存在
	 *
	 * @param account
	 * @return
	 * @throws Exception
	 */
	public boolean existOwnInvitationCode(String account) throws Exception {
		return this.userInfoMapper.existOwnInvitationCode(account);
	}

	/**
	 * 根据根据当前时间查询新用户
	 *
	 * @param nowTime
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserInfo> selectUserByNowTimeTenantCode(String nowTime, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.userInfoMapper.selectUserByNowTime(nowTime);
	}

	/**
	 * 根据邀请码查询员工
	 *
	 * @param ownInvitationCode
	 * @return
	 * @throws Exception
	 */
	public UserInfo selectByOwnInvitationCode(String ownInvitationCode) throws Exception {
		return this.userInfoMapper.selectByOwnInvitationCode(ownInvitationCode);
	}



}
